//
//  JKToast.m
//  LCPackage
//
//  Created by 马军凯 on 2021/6/22.
//  Copyright © 2021 龙采科技集团. All rights reserved.
//

#import "JKToast.h"
#import <QuartzCore/QuartzCore.h>

@implementation JKToast
//初始化
- (id)initWithText:(NSString *)text_ {
    if (self = [super init]) {
        //赋值
        text = [text_ copy];
        //字体大小
        UIFont *font = [UIFont boldSystemFontOfSize:14];
        //字体的尺寸
         CGSize textSize = [text boundingRectWithSize:CGSizeMake(280, MAXFLOAT)
                                              options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                           attributes:@{NSFontAttributeName : font}
                                              context:nil].size;
        //label
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 12, textSize.height + 12)];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.textColor = [UIColor whiteColor];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.font = font;
        textLabel.text = text;
        textLabel.numberOfLines = 0;
        //button
        contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textLabel.frame.size.width, textLabel.frame.size.height)];
        contentView.layer.cornerRadius = 5.0f;
        contentView.layer.borderWidth = 1.0f;
        contentView.layer.borderColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;
        contentView.backgroundColor = [UIColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:0.75f];
        [contentView addSubview:textLabel];
        contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [contentView addTarget:self action:@selector(toastTaped:) forControlEvents:UIControlEventTouchDown];
        contentView.alpha = 0.0f;
        
        duration = DEFAULT_DISPLAY_DURATION;
    }
    return self;
}
//点击事件
- (void)toastTaped:(UIButton *)sender_ {
    [self hideAnimation];
}
//显示
- (void)show {
    UIWindow *window = [self lastWindow];
    contentView.centerX = window.center.x;
    contentView.centerY = window.bounds.size.height - 100;
    [window  addSubview:contentView];
    contentView.layer.zPosition = 10;
    [self showAnimation];
    [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
}
//视图
- (UIWindow *)lastWindow {
    NSArray *windows = [UIApplication sharedApplication].windows;
    for (UIWindow *window in [windows reverseObjectEnumerator]) {
        if ([window isKindOfClass:[UIWindow class]] && CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds)) {
            return window;
        }
    }
    return [UIApplication sharedApplication].keyWindow;
}
//消失
- (void)dismissToast {
    [contentView removeFromSuperview];
}
//显示
- (void)showAnimation {
    [UIView beginAnimations:@"show" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.3];
    contentView.alpha = 1.0f;
    [UIView commitAnimations];
}
//隐藏
- (void)hideAnimation {
    [UIView beginAnimations:@"hide" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(dismissToast)];
    [UIView setAnimationDuration:0.3];
    contentView.alpha = 0.0f;
    [UIView commitAnimations];
}

//时间赋值
- (void)setDuration:(CGFloat)duration_ {
    duration = duration_;
}
- (void)showFromTopOffset:(CGFloat)top_ {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    contentView.center = CGPointMake(window.center.x, top_ + contentView.frame.size.height / 2);
    [window  addSubview:contentView];
    [self showAnimation];
    [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
}

- (void)showFromBottomOffset:(CGFloat)bottom_ {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    contentView.center = CGPointMake(window.center.x, window.frame.size.height - (bottom_ + contentView.frame.size.height / 2));
    [window  addSubview:contentView];
    [self showAnimation];
    [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
}

//显示
+ (void)showWithText:(NSString *)text_ {
    [JKToast showWithText:text_ bottomOffset:100 duration:DEFAULT_DISPLAY_DURATION];
}
//自定义时间显示
+ (void)showWithText:(NSString *)text_ duration:(CGFloat)duration_ {
    JKToast *toast = [[JKToast alloc] initWithText:text_];
    [toast setDuration:duration_];
    [toast show];
}
//自定义距离上面显示
+ (void)showWithText:(NSString *)text_ topOffset:(CGFloat)topOffset_ {
    [JKToast showWithText:text_ topOffset:topOffset_ duration:DEFAULT_DISPLAY_DURATION];
}
//自定义距离上面、时间显示
+ (void)showWithText:(NSString *)text_ topOffset:(CGFloat)topOffset_ duration:(CGFloat)duration_ {
    JKToast *toast = [[JKToast alloc] initWithText:text_];
    [toast setDuration:duration_];
    [toast showFromTopOffset:topOffset_];
}
//自定义距离下面显示
+ (void)showWithText:(NSString *)text_ bottomOffset:(CGFloat)bottomOffset_ {
    [JKToast showWithText:text_ bottomOffset:bottomOffset_ duration:DEFAULT_DISPLAY_DURATION];
}
//自定义距离下面、时间显示
+ (void)showWithText:(NSString *)text_ bottomOffset:(CGFloat)bottomOffset_ duration:(CGFloat)duration_{
    JKToast *toast = [[JKToast alloc] initWithText:text_] ;
    [toast setDuration:duration_];
    [toast showFromBottomOffset:bottomOffset_];
}
@end
